import { AxiosRequestHeaders } from "axios";
import { defineStore } from "pinia";
import { Names } from "./namespace";
import UserInfo, { EmptyUserInfo } from "../entity/interface/user.interface";
import { reactive, ref } from "vue";
import { Local } from "../entity/helper/storage.helper";
import { getDisabledRouting } from "../router/AuthContral";
import { router } from "../router";

export const useUserStore = defineStore(Names.User, () => {
  const userInfo = reactive<UserInfo>((Local.get("userInfo") as UserInfo) || EmptyUserInfo());
  const getUserInfo = () => userInfo;
  const userLogin = (user: UserInfo) => {
    for (const key in user) userInfo[key] = user[key];
    getDisabledRouting().forEach(item => router.removeRoute(item));
    Local.set("userInfo", userInfo);
  };
  const userLogout = () => {
    userInfo.isLogin = false;
    Local.set("userInfo", userInfo);
  };

  return {
    getUserInfo,
    userLogin,
    userLogout,
  };
});
